﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace AutoInsurance
{
    public partial class newInsuredAccident : System.Web.UI.Page
    {
        string strSqlConnection = "Data Source=localhost\\sqlexpress;Initial Catalog=Autoinsurance;Integrated Security=True";
        private int id_manager;
        string id_policy;
        protected void Page_Load(object sender, EventArgs e)
        {
            id_policy = Request.QueryString["id_policy"];

            try
            {
                string role = Session["role"].ToString();
                if (role == "1")
                {
                    GridView1.Visible = false;
                    Panel1.Visible = false;
                    Button2.Visible = false;
                    Label1.Visible = true;
                    Label2.Visible = false;
                }
                else
                {
  
                    SqlConnection sqlCon = new SqlConnection(strSqlConnection);
                    sqlCon.Open();
                    string strQuery = "SELECT * FROM InsuredAccident where id_policy=" + id_policy;
                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = new SqlCommand(strQuery, sqlCon);
                    da.Fill(ds, "InsuredAccident");
                    GridView1.DataSource = ds.Tables["InsuredAccident"];
                    this.DataBind();

                    sqlCon.Close();
                }
            }

            catch (Exception ex)
            {
                Server.Transfer("Error.aspx?error=" + ex.Message);
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String id_accident = GridView1.SelectedRow.Cells[1].Text;
                string myUrl = "newPayout.aspx?id_accident=" + id_accident;
                Response.Redirect(myUrl);
            }
            catch (Exception ex)
            {
                Server.Transfer("Error.aspx?error=" + ex.Message);
            }
             
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {

                //string id_policy = Request.QueryString["id_policy"];
                SqlConnection connection = new SqlConnection(strSqlConnection);
                connection.Open();

                SqlCommand searchManager = new SqlCommand("select * from Manager where login=" + "'" + Session["login"] + "'", connection);
                SqlDataReader id_m = searchManager.ExecuteReader();
                id_m.Read();
                id_manager = id_m.GetInt32(0);
                connection.Close();
                connection.Open();
                SqlCommand newInsuredAccident = new SqlCommand("insert into InsuredAccident (dateofaccident ,id_policy, damage, id_manager)" +
                    "VALUES ('" + Convert.ToDateTime(TextBox1.Text) + "','" + id_policy + "','" + TextBox2.Text + "','" + id_manager + "')", connection);
                newInsuredAccident.ExecuteNonQuery();

                connection.Close();
                string myUrl = "newInsuredAccident.aspx?id_policy=" + id_policy;
                Response.Redirect(myUrl);
            }
            catch (Exception ex)
            {
                Server.Transfer("Error.aspx?error=" + ex.Message);
            }
        }
    }
}